// pages/video/index.js
import ImgListCell from '../../template/ImgListCell';
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    dataList: [],
    // 显示加载更多 loading
    hothidden: true,
    loadinghidden: true,
    isAddBtnShow: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    Object.assign(this, ImgListCell);
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    that.doLoadData();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    var refreshList = wx.getStorageSync("refreshList");
    if (refreshList == "video") {
      that.doLoadData();
      wx.setStorageSync("refreshList", "");
    }

  },
  //加载数据
  doLoadData: function () {
    var that = this;

    var parameter = {};
    parameter['page'] = 1;
    parameter['type'] = 2;
    parameter['order'] = 1;

    /**
     * 显示 loading
     */
    that.setData({
      page: 1,
      loadinghidden: false,
      hothidden: false,
    });

    http.ApiGet(apiUrl.UrlNewsList, parameter, function (res) {
      wx.stopPullDownRefresh(); //停止下拉刷新
      wx.hideNavigationBarLoading(); //完成停止加载
      that.setData({
        loadinghidden: true
      })
      if (res.result.errorCode == 1) {
        var dataList = res.data;
        if (dataList.length > 0) {
          dataList = dataList.map(function (item) {

            item.thumb = item.images;

            if (item.video.length > 0) {
              item.has_video = 1;

              if (item.video_cover.length == 0) {
                item.video_cover = item.video + "?x-oss-process=video/snapshot,t_1,f_jpg,w_0,h_0,m_fast";
              }
            }

            item.autor = item.ymh_name?item.ymh_name:"";

            item.ptime = util.getDateDiff(item.publictime+"000");

            return item;
          })
        }

        that.setData({
          page: 1,
          hothidden: false,
          dataList: dataList
        });
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.doLoadData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;
    // 获取当前现有数据进行保存
    var list = that.data.dataList;
    var parameter = {};
    parameter['page'] = page;
    parameter['type'] = 2;
    parameter['order'] = 1;

    http.ApiGet(apiUrl.UrlNewsList, parameter, function (res) {
      if (res.result.errorCode == 1) {
        var newData = res.data;
        if (newData.length > 0) {
          that.setData({
            page: page,
            hothidden: false,
            dataList: list.concat(newData.map(function (item) {

              item.thumb = item.images;

              if (item.video.length > 0) {
                item.has_video = 1;

                if (item.video_cover.length == 0) {
                  item.video_cover = item.video + "?x-oss-process=video/snapshot,t_1,f_jpg,w_0,h_0,m_fast";
                }
              }

              item.autor = item.ymh_name?item.ymh_name:"";

              item.ptime = util.getDateDiff(item.publictime+"000");

              return item;
            }))
          });
        } else {
          that.setData({
            hothidden: false
          })
        }
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  //列表点击
  listCellTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.id);
    wx.navigateTo({
      url: '../news/detail?id=' + item.id
    })
  },
  addBtnTap: function () {
    wx.navigateTo({
      url: '../video/submit?type=2&title=提交视频'
    })
  }
})